<template>
  <div class="book-detail">
    <el-container>
      <el-header>
        <el-menu mode="horizontal" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b" router>
          <div class="logo">
            <i class="el-icon-reading"></i>
            校园二手书交易平台
          </div>
          <el-menu-item index="/home">首页</el-menu-item>
          <el-menu-item index="/market">书籍市场</el-menu-item>
          <el-menu-item index="/my-posts">我的发布</el-menu-item>
          <el-menu-item index="/messages">消息中心</el-menu-item>
        </el-menu>
      </el-header>

      <el-main v-loading="loading">
        <div v-if="book" class="book-detail-container">
          <el-row :gutter="40">
            <el-col :span="10">
              <div class="book-image-wrapper">
                <img :src="book.imageUrl" :alt="book.title" class="book-image">
                <div class="book-status" :class="book.status === '在售' ? 'status-available' : 'status-sold'">
                  {{ book.status }}
                </div>
              </div>
            </el-col>
            <el-col :span="14">
              <div class="book-info">
                <h1 class="book-title">{{ book.title }}</h1>
                <div class="book-meta">
                  <div class="meta-item">
                    <i class="el-icon-user"></i>
                    <span>作者：{{ book.author }}</span>
                  </div>
                  <div class="meta-item">
                    <i class="el-icon-price-tag"></i>
                    <span class="price">¥{{ book.price }}</span>
                  </div>
                  <div class="meta-item">
                    <i class="el-icon-goods"></i>
                    <span>成色：<el-tag :type="getConditionType(book.bookCondition)" size="medium">{{ book.bookCondition }}</el-tag></span>
                  </div>
                  <div class="meta-item">
                    <i class="el-icon-time"></i>
                    <span>发布时间：{{ formatDate(book.createTime) }}</span>
                  </div>
                </div>
                <div class="book-description">
                  <h3>书籍描述</h3>
                  <p>{{ book.description }}</p>
                </div>
                <div class="action-buttons">
                  <el-button type="primary" size="large" @click="handleContact" icon="el-icon-message">
                    联系卖家
                  </el-button>
                  <el-button 
                    :type="isFavorite ? 'danger' : 'success'" 
                    size="large" 
                    @click="handleFavorite" 
                    :loading="favoriteLoading"
                    :icon="isFavorite ? 'el-icon-star-off' : 'el-icon-star-on'">
                    {{ isFavorite ? '取消收藏' : '收藏' }}
                  </el-button>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
        <div v-else class="no-data">
          <el-empty description="未找到相关书籍信息"></el-empty>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'BookDetail',
  data() {
    return {
      loading: false,
      favoriteLoading: false,
      book: null,
      isFavorite: false
    }
  },
  methods: {
    getConditionType(bookCondition) {
      const types = {
        '全新': 'success',
        '9成新': 'success',
        '8成新': 'warning',
        '7成新': 'info'
      }
      return types[bookCondition] || 'info'
    },
    async fetchBookDetail() {
      this.loading = true
      try {
        const bookId = this.$route.params.id
        const res = await request.get(`/api/home/books/${bookId}`)
        if (res.data.code === 1) {
          this.book = res.data.data
          this.checkFavoriteStatus()
        } else {
          this.$message.error(res.data.msg || '获取书籍详情失败')
        }
      } catch (error) {
        this.$message.error('获取书籍详情失败')
      } finally {
        this.loading = false
      }
    },
    async checkFavoriteStatus() {
      try {
        const res = await request.get(`/api/home/books/${this.book.id}/favorite/status`)
        if (res.data.code === 1) {
          this.isFavorite = res.data.data
        }
      } catch (error) {
        console.error('检查收藏状态失败', error)
      }
    },
    async handleFavorite() {
      this.favoriteLoading = true
      try {
        const action = this.isFavorite ? 'remove' : 'add'
        const res = await request.post(`/api/home/books/${this.book.id}/favorite/${action}`)
        if (res.data.code === 1) {
          this.isFavorite = !this.isFavorite
          this.$message.success(this.isFavorite ? '收藏成功' : '取消收藏成功')
        } else {
          this.$message.error(res.data.msg || '操作失败')
        }
      } catch (error) {
        this.$message.error('操作失败')
      } finally {
        this.favoriteLoading = false
      }
    },
    handleContact() {
      this.$router.push(`/messages?bookId=${this.book.id}`)
    },
    formatDate(date) {
      return new Date(date).toLocaleString()
    }
  },
  created() {
    this.fetchBookDetail()
  }
}
</script>

<style scoped>
.book-detail {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e7eb 100%);
}

.el-header {
  padding: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  background: linear-gradient(to right, #545c64, #4a4f55);
}

.logo {
  float: left;
  line-height: 60px;
  margin: 0 20px;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  display: flex;
  align-items: center;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
}

.logo i {
  margin-right: 10px;
  font-size: 24px;
  color: #ffd04b;
}

.book-detail-container {
  background: #fff;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  margin: 20px auto;
  max-width: 1200px;
  transition: all 0.3s ease;
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.book-detail-container:hover {
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.12);
}

.book-image-wrapper {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
}

.book-image-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.2);
}

.book-image {
  width: 100%;
  height: 600px;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.book-image:hover {
  transform: scale(1.08);
}

.book-status {
  position: absolute;
  top: 20px;
  right: 20px;
  padding: 8px 20px;
  border-radius: 30px;
  color: #fff;
  font-weight: bold;
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(4px);
  transition: all 0.3s ease;
  z-index: 1;
}

.book-status:hover {
  transform: scale(1.05);
}

.status-available {
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
}

.status-sold {
  background: linear-gradient(135deg, #f56c6c 0%, #f78989 100%);
}

.book-info {
  padding: 20px 0;
}

.book-title {
  font-size: 32px;
  color: #303133;
  margin-bottom: 30px;
  font-weight: bold;
  position: relative;
  padding-bottom: 15px;
  background: linear-gradient(45deg, #303133, #606266);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.book-title::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 60px;
  height: 4px;
  background: linear-gradient(to right, #409EFF, #67c23a);
  border-radius: 2px;
}

.book-meta {
  margin-bottom: 30px;
  background: #f8f9fa;
  padding: 25px;
  border-radius: 12px;
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.05);
}

.meta-item {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  color: #606266;
  font-size: 16px;
  transition: all 0.3s ease;
}

.meta-item:last-child {
  margin-bottom: 0;
}

.meta-item:hover {
  transform: translateX(5px);
  color: #409EFF;
}

.meta-item i {
  margin-right: 12px;
  font-size: 20px;
  color: #409EFF;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.meta-item:hover i {
  transform: scale(1.1);
  background: rgba(64, 158, 255, 0.2);
}

.price {
  color: #f56c6c;
  font-size: 28px;
  font-weight: bold;
  text-shadow: 1px 1px 2px rgba(245, 108, 108, 0.2);
}

.book-description {
  margin: 30px 0;
  padding: 25px;
  background: #f8f9fa;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  border: 1px solid rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.book-description:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
  transform: translateY(-2px);
}

.book-description h3 {
  color: #303133;
  margin-bottom: 20px;
  font-size: 20px;
  font-weight: bold;
  position: relative;
  padding-left: 15px;
}

.book-description h3::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 20px;
  background: #409EFF;
  border-radius: 2px;
}

.book-description p {
  color: #606266;
  line-height: 1.8;
  font-size: 16px;
  text-align: justify;
}

.action-buttons {
  margin-top: 40px;
  display: flex;
  gap: 20px;
}

.action-buttons .el-button {
  flex: 1;
  height: 54px;
  font-size: 16px;
  border-radius: 27px;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  position: relative;
  overflow: hidden;
}

.action-buttons .el-button::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  transition: width 0.6s ease, height 0.6s ease;
}

.action-buttons .el-button:hover::after {
  width: 300px;
  height: 300px;
}

.action-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
}

.action-buttons .el-button i {
  margin-right: 8px;
  font-size: 18px;
}

.no-data {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 400px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  margin: 20px;
  animation: fadeIn 0.5s ease-out;
}

/* 自定义标签样式 */
.el-tag {
  font-weight: 500;
  padding: 0 12px;
  height: 28px;
  line-height: 26px;
  border-radius: 14px;
  transition: all 0.3s ease;
}

.el-tag:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style> 